<?php
/**
 * 功能：后台菜单控制器
 * 作者：魏安来
 * 日期：2017/01/16
 */

namespace app\index\controller;
use app\index\controller\Menu;
use app\index\model\AuthRule as AuthRuleModel;
use think\Db;

class AuthRule extends Base {

	/*权限列表*/
	public function index() {
		//调用左侧菜单
		$menuController = new Menu();
		$menuController->sidebar();
		$authRuleModel = new AuthRuleModel();
		$authRuleList = $authRuleModel->getAllRule();
		$this->assign('authRuleList', $authRuleList);
		return $this->fetch();
	}

	/*添加权限*/
	public function add() {
		$authRuleModel = new AuthRuleModel();
		$ruleArr = $authRuleModel->getAllRule();
		$this->assign('ruleArr', $ruleArr);
		if (request()->isPost()) {
			//接收数据
			$data = input('post.');
			if (!isset($data['status'])) {
				$data['status'] = 0;
			} else {
				$data['status'] = 1;
			}
			//定义验证规则
            $rule = [
				'title' => 'require|min:2|unique:auth_rule,title='.$data['title'].'&pid='.$data['pid'], 
				'name' => 'require'
            ];

            //定义错误提示信息
            $message = [
                'title.require' => '权限规则不能为空',
				'title.min' => '权限规则不能少于2个字符',
				'title.unique' => '权限规则已存在',
				'name.require' => '权限名称不能为空'
            ];
			$validate = new \think\Validate($rule, $message);
            $result = $validate->check($data);
			if ($result) {
				//写入数据库
				$affected = db('auth_rule')->insert($data);
				if ($affected) {
					return 'succ';
				} else {
					return 'error';
				}
			} else {
				return $validate->getError();
			}
		}
		return $this->fetch();
	}

	/*权限规则编辑*/
	public function edit() {
		$authRuleModel = new AuthRuleModel();
		$ruleArr = $authRuleModel->getAllRule();
		//接收要编辑的权限规则id
		$id = request()->param('id');
		//查找上级权限规则
		$result=Db::name('auth_rule')->where('id', $id)->find();
		$this->assign('pid', $result['pid']);
		$this->assign('ruleArr', $ruleArr);
		$this->assign('id', $id);
		/*获取需要编辑的权限规则数据*/
		$oneRuleData = $authRuleModel->getOneRuleData($id);
		$this->assign('oneRuleData',$oneRuleData);
		if (request()->isAjax()) {
			$data = input('post.');
			$id = $data['id'];
			unset($data['id']);
			//处理是否显示字段
			if (!isset($data['status'])) {
				$data['status'] = 0;
			} else {
				$data['status'] = 1;
			}
			//如果没有修改权限规则就不验证权限规则是否已存在
			if (trim($data['title']) == trim($result['title'])) {
				//定义验证规则
	            $rule = [
					'title' => 'require|min:2', 
					'name' => 'require'
	            ];

	            //定义错误提示信息
	            $message = [
	                'title.require' => '权限规则不能为空',
					'title.min' => '权限规则不能少于2个字符',
					'name.require' => '权限名称不能为空'
	            ];
			} else {
				//定义验证规则
	            $rule = [
					'title' => 'require|min:2|unique:auth_rule,title='.$data['title'].'&pid='.$data['pid'], 
					'name' => 'require'
	            ];

	            //定义错误提示信息
	            $message = [
	                'title.require' => '权限规则不能为空',
					'title.min' => '权限规则不能少于2个字符',
					'title.unique' => '权限规则已存在',
					'name.require' => '权限名称不能为空'
	            ];
			}
			
			$validate = new \think\Validate($rule, $message);
            $result = $validate->check($data);
			if ($result){
				$affected = $authRuleModel->updataOneRuleData($id, $data);
				if($affected){
					return 'succ';
				}else{
					return 'error';
				}
			} else {
				return $validate->getError();
			}
		}
		return $this->fetch();
	}

	/*权限删除*/
	public function delete() {
		$delete_id = request()->param('id');
		$authRuleModel = new AuthRuleModel();
		$affectRow = $authRuleModel->deleteRule($delete_id);
		if ($affectRow) {
			return 'succ';
		} else {
			return 'error';
		}
	}

}


?>